package com.hm.pan.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hm.pan.model.Admin;
import com.hm.pan.model.ResultObj;
import com.hm.pan.service.AdminService;
	
@RestController
@RequestMapping("admin")
public class AdminController {
	
	@Resource
	private AdminService adminService;
	
	@RequestMapping("update")
	public Object update(Admin admin,String oldadminPassword) {
		//通过获取原来的密码进行判断是否密码输入正确
		String message = adminService.findByPW(oldadminPassword);
		//如果密码正确那么message为空，就进行修改操作
		if(message==null) {
			boolean update = adminService.update(admin);
			return new ResultObj(200,update);
		}
		//密码错误直接方法错误信息；
		return new ResultObj(400,message);
	}
	
	@PostMapping("login")
	public Object logins(String adminName, String adminpassword) {
		
		//通过前端传过来的用户名，判断是否有该用户名
		Admin admin = (Admin) adminService.login(adminName);
		//如果admin不为空说明有改用户名
		if(admin!=null) {
			boolean equals = admin.getAdminPassword().equals(adminpassword);
			if(equals==false) {
				return new ResultObj(400,"密码错误");
			}
		}else {
			return new ResultObj(400,"用户不存在");
		}
		return new ResultObj(200,"admin");
		
	}
	

}
